package mmi.gui.widget;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JToolBar;

import mmi.event.TrialStartedEvent;
import mmi.event.TrialStartedEventListener;
import mmi.event.TrialStoppedEvent;
import mmi.event.TrialStoppedEventListener;
import mmi.gui.Application;
import mmi.gui.MainFrame;

/**
 * 
 * @author louis
 *
 */
public class ExperimentBar extends JToolBar {

	private static final long serialVersionUID = 8690980827076552704L;

	JButton startTrialButton = null;
	JButton endTrialButton = null;
	JButton showTrialButton = null;
	JButton showQuestionnaireButton = null;

	MainFrame parent = null;

	protected javax.swing.event.EventListenerList listenerList = new javax.swing.event.EventListenerList();

	public ExperimentBar(MainFrame parent)
	{
		this.parent = parent;
                setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
                addSeparator();
		add(getStartTrialButton());
                addSeparator();
		add(getEndTrialButton());
                addSeparator();
		add(getShowTrialButton());
                addSeparator();
		add(getShowQuestionnaireButton());

		setFloatable(false);
	}
	public void addTrialStartedEventListener(TrialStartedEventListener listener)
	{
		listenerList.add(TrialStartedEventListener.class, listener);
	}
	public void addTrialStoppedEventListener(TrialStoppedEventListener listener)
	{
		listenerList.add(TrialStoppedEventListener.class, listener);
	}
	void fireTrialStartedEvent(TrialStartedEvent evt)
	{
		Object[] listeners = listenerList.getListenerList();

		// Each listener occupies two elements - the first is the listener class
		// and the second is the listener instance
		for(int i=0; i < listeners.length; i+=2)
		{
			if(listeners[i] == TrialStartedEventListener.class)
			{
				((TrialStartedEventListener)listeners[i+1]).trialStartedEventOccured(evt);
			}
		}
	}
	void fireTrialStoppedEvent(TrialStoppedEvent evt)
	{
		Object[] listeners = listenerList.getListenerList();

		// Each listener occupies two elements - the first is the listener class
		// and the second is the listener instance
		for(int i=0; i < listeners.length; i+=2)
		{
			if(listeners[i] == TrialStoppedEventListener.class)
			{
				((TrialStoppedEventListener)listeners[i+1]).trialStoppedEventOccured(evt);
			}
		}
	}

	/**
	 * 
	 * @return
	 */
	private JButton getEndTrialButton()
	{
		if(endTrialButton == null)
		{
			endTrialButton = WidgetUtils.initialiseDrawingBarButton("End Trial", "end.png", false);

			endTrialButton.addActionListener(new java.awt.event.ActionListener()
			{
				/*
				 * (non-Javadoc)
				 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
				 */
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					fireTrialStoppedEvent(new TrialStoppedEvent(this));
					Application.TraceDebug("Trial ended");
				}
			});
		}
		return endTrialButton;
	}

	/**
	 * 
	 * @return
	 */
	private JButton getShowQuestionnaireButton()
	{
		if(showQuestionnaireButton == null)
		{
			showQuestionnaireButton = WidgetUtils.initialiseDrawingBarButton("Questionnaire", "questions.png", false);

			showQuestionnaireButton.addActionListener(new java.awt.event.ActionListener()
			{
				/*
				 * (non-Javadoc)
				 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
				 */
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					parent.questionnaireFrame.showFrame();
				}
			});
		}
		return showQuestionnaireButton;
	}

	/**
	 * 
	 * @return
	 */
	private JButton getShowTrialButton()
	{
		if(showTrialButton == null)
		{
			showTrialButton = WidgetUtils.initialiseDrawingBarButton("Show Trial", "show.png", false);

			showTrialButton.addActionListener(new java.awt.event.ActionListener()
			{
				/*
				 * (non-Javadoc)
				 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
				 */
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					parent.experimentFrame.showFrame();
				}
			});
		}
		return showTrialButton;
	}

	/**
	 * 
	 * @return
	 */
	private JButton getStartTrialButton()
	{
		if(startTrialButton == null)
		{
			startTrialButton = WidgetUtils.initialiseDrawingBarButton("Start Trial", "start.png", false);

			startTrialButton.addActionListener(new java.awt.event.ActionListener()
			{
				/*
				 * (non-Javadoc)
				 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
				 */
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					fireTrialStartedEvent(new TrialStartedEvent(this));
					Application.TraceDebug("Trial started");
				}
			});
		}
		return startTrialButton;
	}

	public void removeTrialStartedEventListener(TrialStartedEventListener listener)
	{
		listenerList.remove(TrialStartedEventListener.class, listener);
	}

	public void removeTrialStoppedEventListener(TrialStoppedEventListener listener)
	{
		listenerList.remove(TrialStoppedEventListener.class, listener);
	}
}
